using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using RBAC.ErrorCode;
using RBAC.Read.Api.Application.Command.ERP.InboundManagement;
using RBAC.Read.Api.Application.Dto.ERP;

namespace RBAC.Read.Api.Controllers.ERP
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InboundManagementController : ControllerBase
    {
        private readonly IMediator mediator;

        public InboundManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 采购入库分页
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIPaging<PurchaseStorageListDto>> GetPurchaseStoragePage([FromQuery] GetPurchaseStoragePageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return await mediator.Send(request, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIPaging<PurchaseStorageListDto>
                {
                    Code = APIEnums.Error,
                    Msg = $"获取采购入库分页失败: {ex.Message}",
                    PageDatas = new List<PurchaseStorageListDto>(),
                    TotalCount = 0
                };
            }
        }

        /// <summary>
        /// 根据ID获取采购入库详情（含明细）
        /// </summary>
        /// <param name="id">采购入库主表ID</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<APIResult<PurchaseStorageDetailDto>> GetPurchaseStorageById(long id, CancellationToken cancellationToken)
        {
            try
            {
                var cmd = new GetPurchaseStorageByIdCommand { Id = id };
                return await mediator.Send(cmd, cancellationToken);
            }
            catch (Exception ex)
            {
                return new APIResult<PurchaseStorageDetailDto>
                {
                    Code = APIEnums.Error,
                    Msg = $"获取采购入库详情失败: {ex.Message}",
                    Data = null
                };
            }
        }

    }
}
